/* Copyright 2001,2002,2003 NAH6
 * All Rights Reserved
 *
 * Parts Copyright DoD, Parts Copyright Starium
 *
 */
#include "celp_export.h"
#include "main.h"
#include "celpfilt.h"
#include "analysis.h"
#include "codeparm.h"
#include "synth.h"
#include <common.h>
#include <memory.h>

/**************************************************************************
*                                                                         *
*       U.S. Department of Defense                                        *
*       CELP Voice Coder                                                  *
*       Version 3.3c                                                      *
*       1 June 1994                                                       *
*                                                                         *
*       FOR OFFICIAL U.S. GOVERNMENT CONTRACT USE ONLY                    *
*                                                                         *
*       The U.S. Government shall not be held liable for any damages      *
*       resulting from this code.  Further reproduction or distribution   *
*       of this code without prior written permission of the U.S.         *
*       Government is prohibited.  R224 may be contacted at:              *
*                                                                         *
*               DIRNSA, R224                                              *
*               9800 Savage Road                                          *
*               Fort Meade, MD 20755-6000 USA                             *
*                                                                         *
*==========================================================================
*
* REFERENCES
*
*       "Details to Assist in Implementation of Federal Standard 1016 CELP,"
*       Technical Information Bulletin 92-1, National Communications System,
*       (included as a Postscript file in CELP 3.3 Release)
*
*       "Federal Standard 1016," Telecommunications: Analog-to-Digital
*       Conversion of Voice by 4800 bit/second Code Excited Linear
*       Predictive (CELP) Coding, National Communications System, distributed
*       by the General Services Administration:
*           GSA Federal Supply Service Bureau
*           Specification Section, Suite 8100
*           470 E. L'Enfant Place, S.W.
*           Washington, DC 20407
*           (202)755-0325
*
*      Misc: The following articles describe the Federal-Standard-1016
*       4.8-kbps CELP coder (it's unnecessary to read more than one):
*          + Campbell, Joseph P. Jr., Thomas E. Tremain and Vanoy C.
*            Welch, "The Federal Standard 1016 4800 bps CELP Voice Coder,"
*            Digital Signal Processing, Academic Press, 1991, Vol. 1, No.
*            3, p. 145-155.
*          + Campbell, Joseph P. Jr., Thomas E. Tremain and Vanoy C.
*            Welch, "The DoD 4.8 kbps Standard (Proposed Federal Standard
*            1016)," in Advances in Speech Coding, ed. Atal, Cuperman and
*            Gersho, Kluwer Academic Publishers, 1991, Chapter 12, p.
*            121-133.
*          + Campbell, Joseph P. Jr., Thomas E. Tremain and Vanoy C.
*            Welch, "The Proposed Federal Standard 1016 4800 bps Voice
*            Coder: CELP," Speech Technology Magazine, April/May 1990, p.
*            58-64.
*/


#define CELP_EDAC       FALSE
#define CELP_SMOOTHING  FALSE



void celp_init (void)
{
  static bool init_called = FALSE;

  if (!init_called) {
    setup_filters ();
    init_called = TRUE;
  }
}


void
celp_encode (uint8 wirerep[CELP_WIREREP_BYTES],
             const int16 input_const[CELP_FRAME_LENGTH])
{
  static TX_PARAM parameters;
  int16 input[CELP_FRAME_LENGTH];

  /*
   *  The lower level routines do not treat the input as const.
   */
#if 1
  memcpy (input, input_const, sizeof (input));
#else
  {
	  int i;
	  for( i= 0; i<CELP_FRAME_LENGTH; i++ )
		  input[i]= ((int)input_const[i])/2;
  }
#endif

  /*
   * Analyze input speech and calculate  parameters
   */
  Analysis (input, &parameters);

  /* Put analysis parameters into bitstream */
  EncodeParameters (parameters, CELP_EDAC, wirerep);
}



void
celp_decode (short output[CELP_FRAME_LENGTH],
             const uint8 wirerep[CELP_WIREREP_BYTES])
{
  static TX_PARAM parameters;

  /*
   * Get parameters from bitstream
   */
  DecodeParameters(wirerep, CELP_EDAC, CELP_SMOOTHING, &parameters);

  /*
   * Synthesize parameters into output speech
   */
  Synthesis(parameters, output);
}
